<template>
  <div class="dashboard-editor-container">
    <github-corner class="github-corner" />

    <panel-group @handleSetLineChartData="hand" />

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="8">
      <el-col :xs="{span: 24}" :sm="{span: 24}" :md="{span: 24}" :lg="{span: 12}" :xl="{span: 12}" style="padding-right:8px;margin-bottom:30px;">
        <transaction-table />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <todo-list />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <box-card />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import GithubCorner from '@/components/GithubCorner'
import PanelGroup from './components/PanelGroup'
import LineChart from './components/LineChart'
import RaddarChart from './components/RaddarChart'
import PieChart from './components/PieChart'
import BarChart from './components/BarChart'
import TransactionTable from './components/TransactionTable'
import TodoList from './components/TodoList'
import BoxCard from './components/BoxCard'

import { gettime, getuserNum, orderPrice } from '@/api/activityCharge'
var Data = {
  // 用户人数
  newVisitis: {
    actualData: [1, 0, 0, 0, 0, 0, 0]
  },
  // 订单
  messages: {
    actualData: [1, 1, 1, 0, 1, 0, 0]
  },
  // 流水
  purchases: {
    actualData: [10, 10, 0, 10, 11, 0, 30]
  }
}

export default {
  name: 'DashboardAdmin',
  components: {
    GithubCorner,
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    TransactionTable,
    TodoList,
    BoxCard
  },
  data() {
    return {
      // 当前显示那个表的数据
      lineChartData: Data.newVisitis,
      chartList: [], // 用户下单时间列表
      chartList2: [], // 时间表(时间格式)
      timestamp: [], // 时间戳表
      mon: 0, // 星期一
      tue: 0, // 星期二
      wed: 0, // 星期三
      thu: 0, // 星期四
      fri: 0, // 星期五
      sat: 0, // 星期六
      sun: 0, // 星期天
      applyList: [], // 渲染用户注册时间
      userordertimeList: [], // 用户下单时间
      userordertimeList2: [], // 用户下单时间表(时间格式)
      usertimestamp: [], // 用户时间戳
      umon: 0, // 星期一
      utue: 0, // 星期二
      uwed: 0, // 星期三
      uthu: 0, // 星期四
      ufri: 0, // 星期五
      usat: 0, // 星期六
      usun: 0, // 星期天
      userapplyList: [], // 渲染用户下单时间
      totalPriceList: [], // 用户下单金额
      totalPriceList2: [], // 用户下单金额(时间格式)
      priceTamp: [], // 价格时间戳
      totalPrices: []
    }
  },
  mounted() {
    this.gettime() // 用户注册时间分布(星期)
    this.getuserNum() // 用户下单时间分布(星期)
    this.orderPrice() // 用户下单金额
    // 修改折线图
    var usersNum = this.applyList // 用户
    var goodorder = this.userapplyList // 订单
    var priceorder = [20, 0, 17, 10, 0, 0] // 订单价格C
    Data = {
      // 用户人数
      newVisitis: {
        actualData: usersNum
      },
      // 订单
      messages: {
        actualData: goodorder
      },
      // 流水
      purchases: {
        actualData: priceorder
      }
    }
  },
  methods: {
    hand(type) {
      this.lineChartData = Data[type]
    },
    // 获取用户时间
    async gettime() {
      const res = await gettime()
      if (res.code !== 200) return this.$message.error('获取失败')
      this.chartList = res.data
      // console.log(this.chartList, 106)
      for (var i in this.chartList) {
        this.chartList2.push(this.chartList[i].establishTime)
      }
      // console.log(this.chartList2)
      for (var j in this.chartList2) {
        var date = new Date(this.chartList2[j])
        var time1 = date.getDay()
        this.timestamp.push(time1)
      }
      // console.log(this.timestamp, '用户注册时间')
      // 统计星期一出现次数
      for (var k = 0; k < this.timestamp.length; k++) {
        if (this.timestamp[k] === 1) {
          this.mon++
        }
      }
      // console.log(this.mon, '星期一')
      // 统计星期二出现次数
      for (var q = 0; q < this.timestamp.length; q++) {
        if (this.timestamp[q] === 2) {
          this.tue++
        }
      }
      // console.log(this.tue, '星期二')
      // 统计星期三出现次数
      for (var w = 0; w < this.timestamp.length; w++) {
        if (this.timestamp[w] === 3) {
          this.wed++
        }
      }
      // console.log(this.wed, '星期三')
      // 统计星期四出现次数
      for (var e = 0; e < this.timestamp.length; e++) {
        if (this.timestamp[e] === 4) {
          this.thu++
        }
      }
      // console.log(this.thu, '星期四')
      // 统计星期五出现次数
      for (var t = 0; t < this.timestamp.length; t++) {
        if (this.timestamp[t] === 5) {
          this.fri++
        }
      }
      // console.log(this.fri, '星期五')
      // 统计星期六出现次数
      for (var y = 0; y < this.timestamp.length; y++) {
        if (this.timestamp[y] === 6) {
          this.sat++
        }
      }
      // console.log(this.sat, '星期六')
      // 统计星期天出现次数
      for (var u = 0; u < this.timestamp.length; u++) {
        if (this.timestamp[u] === 0) {
          this.sun++
        }
      }
      // console.log(this.sun, '星期天')
      // 添加数据渲染
      this.applyList.push(this.mon)
      this.applyList.push(this.tue)
      this.applyList.push(this.wed)
      this.applyList.push(this.thu)
      this.applyList.push(this.fri)
      this.applyList.push(this.sat)
      this.applyList.push(this.sun)
      // console.log(this.applyList, '渲染星期几列表')
      this.lineChartData = {
        actualData: this.applyList
      }
    },
    // 用户订单数量
    async getuserNum() {
      const res = await getuserNum()
      // if (res.code !== 200) return this.$message.error('获取失败')
      this.userordertimeList = res.data
      for (var i in this.userordertimeList) {
        this.userordertimeList2.push(this.userordertimeList[i].creationtime)
      }
      // console.log(this.userordertimeList2)
      for (var j in this.userordertimeList2) {
        var date = new Date(this.userordertimeList2[j])
        var time = date.getDay()
        this.usertimestamp.push(time)
      }
      // console.log(this.usertimestamp, '用户下单数量时间戳')
      // 统计星期一出现次数
      for (var k = 0; k < this.usertimestamp.length; k++) {
        if (this.usertimestamp[k] === 1) {
          this.umon++
        }
      }
      // 统计星期二出现次数
      for (var q = 0; q < this.usertimestamp.length; q++) {
        if (this.usertimestamp[q] === 2) {
          this.utue++
        }
      }
      // 统计星期三出现次数
      for (var w = 0; w < this.usertimestamp.length; w++) {
        if (this.usertimestamp[w] === 3) {
          this.uwed++
        }
      }
      // 统计星期四出现次数
      for (var e = 0; e < this.usertimestamp.length; e++) {
        if (this.usertimestamp[e] === 4) {
          this.uthu++
        }
      }
      // 统计星期五出现次数
      for (var t = 0; t < this.usertimestamp.length; t++) {
        if (this.usertimestamp[t] === 5) {
          this.ufri++
        }
      }
      // 统计星期六出现次数
      for (var y = 0; y < this.usertimestamp.length; y++) {
        if (this.usertimestamp[y] === 6) {
          this.usat++
        }
      }
      // 统计星期天出现次数
      for (var u = 0; u < this.usertimestamp.length; u++) {
        if (this.usertimestamp[u] === 0) {
          this.usun++
        }
      }
      this.userapplyList.push(this.umon)
      this.userapplyList.push(this.utue)
      this.userapplyList.push(this.uwed)
      this.userapplyList.push(this.uthu)
      this.userapplyList.push(this.ufri)
      this.userapplyList.push(this.usat)
      this.userapplyList.push(this.usun)
      // console.log(this.userapplyList, 77777)
    },
    // 用户下单订单价格
    async orderPrice() {
      const res = await orderPrice()
      if (res.code !== 200) return this.$message.error('获取失败')
      this.totalPriceList = res.data
      // console.log(this.totalPriceList)
      for (var i in this.totalPriceList) {
        this.totalPriceList2.push(this.totalPriceList[i].creationtime)
        this.totalPriceList2.push(this.totalPriceList[i].price)
      }
      // console.log(this.totalPriceList2, 88888888)
      for (var j in this.totalPriceList2) {
        var date = new Date(this.chartList2[j])
        var time5 = date.getDay()
        this.priceTamp.push(time5)
      }
      // console.log(this.priceTamp, 9999999999)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
